package com.k.rabbitmq.amqp.demo;

import com.k.rabbitmq.RabbitMqConst;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;

public class ReceiveLogs {

    public static void main(String[] argv) throws Exception {
        System.out.println("Consumer start");
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("10.110.12.180");
        factory.setUsername("admin");
        factory.setPassword("Command_123");
        factory.setPort(27000);
        factory.setVirtualHost("mcc_vhost");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        // 声明 Exchange
        //channel.exchangeDeclare(RabbitMqConst.ExchangeName.ALARM_TYPE, RabbitMqConst.ExchangeType.FANOUT);
        // 声明 Queue
        channel.queueDeclare(RabbitMqConst.QueueName.ALARM_TYPE_QUEUE,true,false,true,null);

        channel.queueBind(RabbitMqConst.QueueName.ALARM_TYPE_QUEUE, RabbitMqConst.ExchangeName.ALARM_TYPE, "");

        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), "UTF-8");
            System.out.println(" [x] Received '" + message + "'");
        };
        channel.basicConsume(RabbitMqConst.QueueName.ALARM_TYPE_QUEUE, true, deliverCallback, consumerTag -> {
        });
        System.out.println("Consumer finish");
    }
}